package com.example.fsd.service.jdal;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.fsd.common.result.R;
import com.example.fsd.pojo.ConfigMap;
import com.example.fsd.pojo.Jdal;
import com.example.fsd.mapper.JdalMapper;
import com.example.fsd.service.BaseService;
import com.example.fsd.service.jdal.IJdalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.fsd.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhangguihong
 * @since 2024-11-22
 */
@Service
public class JdalServiceImpl extends ServiceImpl<JdalMapper, Jdal> implements IJdalService, BaseService {

    @Override
    public R listjdalPage(PageVo pageVo) {
        LambdaQueryWrapper<Jdal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Jdal::getIsShow, "1");
        Page<Jdal> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        IPage<Jdal> jdalIPage = this.page(page, lambdaQueryWrapper);
        return R.ok().put("jdalIPage", jdalIPage);
    }

    @Override
    public R findjdalById(Integer id) {
        return R.ok().put("jdal", this.getById(id));
    }

    @Override
    public R findjdalnextById(Integer id) {
        LambdaQueryWrapper<Jdal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.gt(Jdal::getId, id);
        lambdaQueryWrapper.eq(Jdal::getIsShow, "1");
        lambdaQueryWrapper.last("limit 1");
        Jdal one = this.getOne(lambdaQueryWrapper);
        if (one == null) {
            return R.error(1404, "没有下一篇");
        }
        return R.ok().put("jdal", one);
    }

    @Override
    public R findjdalprevById(Integer id) {
        LambdaQueryWrapper<Jdal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.lt(Jdal::getId, id);
        lambdaQueryWrapper.orderByDesc(Jdal::getId);
        lambdaQueryWrapper.eq(Jdal::getIsShow, "1");
        lambdaQueryWrapper.last("limit 1");
        Jdal one = this.getOne(lambdaQueryWrapper);
        if (one == null) {
            return R.error(1404, "没有上一篇");
        }
        return R.ok().put("jdal", one);
    }

    @Override
    public R findAlljdal() {
        //降序排列
        List<Jdal> list = this.list(new LambdaQueryWrapper<Jdal>().eq(Jdal::getIsShow, "1").orderByDesc(Jdal::getId));
        return R.ok().put("list", list);
    }

    @Override
    public R addJdal(Jdal jdal) {
        jdal.setCreateTime(new Date());
        boolean save = this.save(jdal);
        if (!save) {
            return R.error(1400, "添加失败");
        }
        return R.ok();
    }

    @Override
    public R handleImage(MultipartFile file) {
        System.out.println("updateBanner:"+file.getOriginalFilename());
        // 确保文件不为空
        if (file == null || file.isEmpty()) {
            return R.error("上传的文件不能为空");
        }
        // 获取文件名和扩展名
        String originalFilename = file.getOriginalFilename();
        if (originalFilename == null) {
            return R.error("无法获取文件名");
        }

        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));

        // 校验扩展名
        if (!fileExtension.equals(".jpg") &&!fileExtension.equals(".png") &&!fileExtension.equals(".jpeg") && !fileExtension.equals(".gif")) {
            return R.error("上传的文件扩展名不正确");
        }
        //创建新的文件名
        String newFilename = System.currentTimeMillis() + fileExtension;

        // 拼接绝对路径，确保使用完整路径
        String basePath = "/www/wwwroot/fsdadminapi.kfcit.com/upload"; // 修改为合适的绝对路径
        String filePath = basePath + "/" + newFilename;
        System.out.println(filePath);

        // 创建目录
        File directory = new File(basePath);
        if (!directory.exists()) {
            boolean dirCreated = directory.mkdirs(); // 创建多级目录
            if (dirCreated) {
                System.out.println("Directory created: " + directory.getAbsolutePath());
            } else {
                return R.error("无法创建目录，请检查权限");
            }
        }

        try {
            // 保存文件
            file.transferTo(new File(filePath));
        } catch (IOException e) {
            e.printStackTrace(); // 打印异常堆栈
            return R.error("文件保存失败: " + e.getMessage());
        }
        // 返回完整的 URL
        String fileUrl = "/upload/" + newFilename; // 根据实际情况修改服务器地址和端口
        return R.ok().put("url", fileUrl);
    }

    @Override
    public R updateCasexqImg(MultipartFile file, Integer id) {
        System.out.println("updateBanner:"+file.getOriginalFilename());
        // 确保文件不为空
        if (file == null || file.isEmpty()) {
            return R.error("上传的文件不能为空");
        }
        // 获取文件名和扩展名
        String originalFilename = file.getOriginalFilename();
        if (originalFilename == null) {
            return R.error("无法获取文件名");
        }

        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));

        // 校验扩展名
        if (!fileExtension.equals(".jpg") &&!fileExtension.equals(".png") &&!fileExtension.equals(".jpeg") && !fileExtension.equals(".gif")) {
            return R.error("上传的文件扩展名不正确");
        }
        //创建新的文件名
        String newFilename = System.currentTimeMillis() + fileExtension;

        // 拼接绝对路径，确保使用完整路径
        String basePath = "/www/wwwroot/fsdadminapi.kfcit.com/upload"; // 修改为合适的绝对路径
        String filePath = basePath + "/" + newFilename;
        System.out.println(filePath);

        // 创建目录
        File directory = new File(basePath);
        if (!directory.exists()) {
            boolean dirCreated = directory.mkdirs(); // 创建多级目录
            if (dirCreated) {
                System.out.println("Directory created: " + directory.getAbsolutePath());
            } else {
                return R.error("无法创建目录，请检查权限");
            }
        }

        try {
            // 保存文件
            file.transferTo(new File(filePath));
        } catch (IOException e) {
            e.printStackTrace(); // 打印异常堆栈
            return R.error("文件保存失败: " + e.getMessage());
        }
        // 更新图片地址
        Jdal jdal = new Jdal();
        jdal.setId(id);
        jdal.setImgUrl("/upload/" + newFilename);
        boolean update = this.updateById(jdal);
        if (!update) {
            return R.error(1404, "更新失败");
        }
        // 返回完整的 URL
        String fileUrl = "/upload/" + newFilename; // 根据实际情况修改服务器地址和端口
        return R.ok().put("url", fileUrl);
    }

    @Override
    public R updateJdal(Jdal jdal) {
        boolean update = this.updateById(jdal);
        if (!update) {
            return R.error(1404, "更新失败");
        }
        return R.ok();
    }

}
